﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class CubeBody : MonoBehaviour
{
    private Transform lastBody;
    private Vector3 lastPos, startPos;
    private Quaternion lastRotation, startRot;
    float dis = 0;
    float needTime, timer;
    public void Init(Transform lastBody)
    {
        this.lastBody = lastBody;
        lastPos = lastBody.position;
        startPos = transform.position;
        dis = Vector3.Distance(transform.position, lastPos);
        lastRotation = Quaternion.LookRotation(lastPos - startPos);
        startRot = transform.rotation;
        needTime = dis / 5f;
    }
    private void Update()
    {
        timer += Time.deltaTime;
        if (Vector3.Distance(transform.position, lastPos) > 0.01f)
        {
            transform.rotation = Quaternion.Lerp(startRot, lastRotation, timer / 0.5f);
            transform.position = Vector3.Lerp(startPos, lastPos, timer / needTime);
        }
        else
        {
            lastPos = lastBody.position;
            startPos = transform.position;
            lastRotation = Quaternion.LookRotation(lastPos - startPos);
            startRot = transform.rotation;
            timer = 0;
        }
    }
}
